#include "wxsmenubar.h"
#include "wxsmenueditor.h"
#include "wxsmenu.h"
#include "../wxsitemresdata.h"
#include <wx/menu.h>
#include "scrollingdialog.h"

#include <prep.h>

namespace {
  wxsRegisterItem<wxsMenuBar> Reg( _T( "MenuBar" ), _T( "MenuBar" ), wxsTTool, _T( "工具" ), 60 );
  class MenuEditorDialog: public wxScrollingDialog {
    public:
      wxsMenuEditor* Editor;
      MenuEditorDialog( wxsMenuBar* MenuBar ):
        wxScrollingDialog( 0, -1, _T( "MenuBar editor" ), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) {
        wxBoxSizer* Sizer = new wxBoxSizer( wxVERTICAL );
        Sizer->Add( Editor = new wxsMenuEditor( this, MenuBar ), 1, wxEXPAND, 0 );
        Sizer->Add( CreateButtonSizer( wxOK | wxCANCEL ), 0, wxEXPAND, 15 );
        SetSizer( Sizer );
        Sizer->SetSizeHints( this );
        PlaceWindow( this, pdlCentre, true );
      }
      void OnOK( M_Code_unused wxCommandEvent& event ) {
        Editor->ApplyChanges();
        EndModal( wxID_OK );
      }

      DECLARE_EVENT_TABLE()
  };

  BEGIN_EVENT_TABLE( MenuEditorDialog, wxScrollingDialog )
    EVT_BUTTON( wxID_OK, MenuEditorDialog::OnOK )
  END_EVENT_TABLE()
}

wxsMenuBar::wxsMenuBar( wxsItemResData* Data ):
  wxsTool( Data, &Reg.Info, 0, 0, flVariable | flSubclass | flExtraCode ) {
}

void wxsMenuBar::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
  case wxsCPP:
    AddHeader( _T( "<wx/menu.h>" ), GetInfo().ClassName, hfInPCH );
    Codef( _T( "%C();\n" ) );
    for( int i = 0; i < GetChildCount(); i++ ) {
      GetChild( i )->BuildCode( GetCoderContext() );
    }
    Codef( _T( "%MSetMenuBar(%O);\n" ) );
    BuildSetupWindowCode();
    break;
  case wxsUnknownLanguage:
  default:
    wxsCodeMarks::Unknown( _T( "wxsMenuBar::OnBuildCreatingCode" ), GetLanguage() );
  }
}

void wxsMenuBar::OnEnumToolProperties( M_Code_unused long Flags ) {
}

bool wxsMenuBar::OnCanAddToResource( wxsItemResData* Data, bool ShowMessage ) {
  if( Data->GetClassType() != _T( "wxFrame" ) ) {
    if( ShowMessage ) {
      msging( _T( "wxMenuBar can be added to wxFrame only" ) );
    }
    return false;
  }
  for( int i = 0; i < Data->GetToolsCount(); i++ ) {
    if( Data->GetTool( i )->GetClassName() == _T( "wxMenuBar" ) ) {
      if( ShowMessage ) {
        msging( _T( "Can not add two or more wxMenuBar classes\ninto one wxFrame" ) );
      }
      return false;
    }
  }
  return true;
}

bool wxsMenuBar::OnXmlReadChild( TiXmlElement* Elem, bool IsXRC, bool IsExtra ) {
  if( IsXRC ) {
    wxString ClassName = cbC2U( Elem->Attribute( "class" ) );
    if( ClassName == _T( "wxMenu" ) ) {
      wxsMenu* Child = new wxsMenu( GetResourceData() );
      if( !AddChild( Child ) ) {
        delete Child;
        return false;
      }
      return Child->XmlRead( Elem, IsXRC, IsExtra );
    }
  }
  return true;
}

bool wxsMenuBar::OnCanAddChild( wxsItem* Item, bool ShowMessage ) {
  if( Item->GetInfo().ClassName != _T( "wxMenu" ) ) {
    if( ShowMessage ) {
      msging( _T( "Only wxMenu items can be added into wxMenuBar" ) );
    }
    return false;
  }
  return true;
}

bool wxsMenuBar::OnMouseDClick( M_Code_unused wxWindow* Preview, M_Code_unused int PosX, M_Code_unused int PosY ) {
  MenuEditorDialog Dlg( this );
  Dlg.ShowModal();
  return false;
}
